package com.cxmx.escort.common.redis;

import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class CacheUtils {

    @Resource
    private RedisTemplate<String, String> redisTemplate;
    private static CacheUtils cacheUtils;

    @PostConstruct
    public void init() {
        cacheUtils = this;
        cacheUtils.redisTemplate = this.redisTemplate;
    }

    /**
     * 保存到hash集合中
     *
     * @param hName 集合名
     * @param key
     * @param value
     */
    public static void hashSet(String hName, String key, Object value, long ex) {
        cacheUtils.redisTemplate.opsForHash().put(hName, key, value);
        if (ex != -1) {
            cacheUtils.redisTemplate.expire(hName, ex, TimeUnit.SECONDS);
        }
    }

    /**
     * 从hash集合里取得
     *
     * @param hName
     * @param key
     * @return
     */

    public static Object hashGet(String hName, String key) {
        return cacheUtils.redisTemplate.opsForHash().get(hName, key);
    }

    /**
     * 计数器
     *
     * @param key
     * @param increasing
     * @return
     */
    public static Long incSet(String key, int increasing) {
        Long o = cacheUtils.redisTemplate.opsForValue().increment(key, increasing);
        return o;
    }

    /**
     * 计数器(递增1)
     *
     * @param key
     * @return
     */
    public static Long incSet(String key) {
        Long o = cacheUtils.redisTemplate.opsForValue().increment(key, 1);
        return o;
    }

    /**
     * 计数器
     *
     * @param key
     * @param increasing
     * @return
     */
    public static Long incSet(String key, int increasing, long ex, TimeUnit r) {
        incSet(key, increasing);
        cacheUtils.redisTemplate.expire(key, ex, r);
        return cacheUtils.redisTemplate.getExpire(key);
    }

    /**
     * 计数器
     *
     * @param key
     * @param increasing
     * @return
     */
    public static Long incSet(String key, int increasing, long ex) {
        incSet(key, increasing);
        if (incGet(key) == 0) {
            cacheUtils.redisTemplate.expire(key, ex, TimeUnit.SECONDS);
        }
        System.out.println("过期时间为:" + cacheUtils.redisTemplate.getExpire(key));
        return cacheUtils.redisTemplate.getExpire(key);
    }


    /**
     * 过期时间设置
     *
     * @param key
     * @param ex
     * @param t
     * @return
     */
    public static Long expire(String key, long ex, TimeUnit t) {
        cacheUtils.redisTemplate.expire(key, ex, t);
        return cacheUtils.redisTemplate.getExpire(key);
    }

    /**
     * 获取计算器
     *
     * @param key
     * @return
     */
    public static Long incGet(String key) {
        Object o = cacheUtils.redisTemplate.opsForValue().get(key);
        if (null == o) {
            return 0L;
        }
        return Long.parseLong(o.toString());
    }

//    public  long incGet(final String key) {
//        return redisTemplate.execute(new RedisCallback<Long>() {
//            @Override
//            public Long doInRedis(RedisConnection connection) throws DataAccessException {
//                RedisSerializer<String> serializer=redisTemplate.getStringSerializer();
//                byte[] rowkey=serializer.serialize(key);
//                byte[] rowval=connection.get(rowkey);
//                try {
//                    String val=serializer.deserialize(rowval);
//                    if(null==val){
//                        return 0L;
//                    }else{
//                        return Long.parseLong(val);
//                    }
//
//                } catch (Exception e) {
//                    return 0L;
//                }
//            }
//        });
//    }

    /**
     * 从hash模糊查询
     *
     * @param hname
     * @param key
     * @return
     */

    public static List<Object> hashDimGet(String hname, String key) {
        Cursor<Map.Entry<Object, Object>> curosr = cacheUtils.redisTemplate.opsForHash().scan(hname, ScanOptions.scanOptions().match(key).build());
//        Cursor<Map.Entry<Object, Object>> curosr = cacheUtils.redisTemplate.opsForHash().entries(hname);
        List<Object> list = new ArrayList<>();
        while (curosr.hasNext()) {
            Map.Entry<Object, Object> entry = curosr.next();
            list.add(entry.getValue());
//            DumboUser user= (DumboUser) entry.getValue();
//            System.out.println(entry.getKey()+":"+user.getAge());
        }
//        Map<Object, Object> entries = cacheUtils.redisTemplate.opsForHash().entries(hname);
//        System.out.println(entries);
        return list;
    }


    public static void setZset(String key, String value, long score) {
        cacheUtils.redisTemplate.opsForZSet().add(key, value, score);
    }

    public static void setTimeOut(String key, long t) {
        cacheUtils.redisTemplate.expire(key, t, TimeUnit.SECONDS);
    }

    public static List<String> getZset(String key, long start, long end) {
        Set<String> setlist = cacheUtils.redisTemplate.opsForZSet().range(key, start, end);
        List<String> list = new ArrayList<String>(setlist);
        return list;
    }

    public static void rovemZset(String key, Object o) {
        cacheUtils.redisTemplate.opsForZSet().remove(key, o);
    }

    public static void rovemZset(String key, double var1, double var2) {
        cacheUtils.redisTemplate.opsForZSet().removeRangeByScore(key, var1, var2);

    }

    /**
     * 增加到set
     *
     * @param hName
     * @param key
     * @param value
     */
    public static void setSet(String hName, String key, String value) {
        cacheUtils.redisTemplate.opsForSet().add("");
    }


    /**
     * 追加String
     *
     * @param key
     * @param value
     */
    public static void setString(String key, String value, long ex) {
        if (ex == -1) {
            cacheUtils.redisTemplate.opsForValue().set(key, value);
        } else {
            cacheUtils.redisTemplate.opsForValue().set(key, value, ex, TimeUnit.SECONDS);
        }
    }

    public static void setString(String key, String value, long ex, TimeUnit T) {
        cacheUtils.redisTemplate.opsForValue().set(key, value, ex, T);
    }

    /**
     * 追加String
     *
     * @param key
     */
    public static void deleteKey(String key) {
        cacheUtils.redisTemplate.delete(key);
    }


    /**
     * 删除
     *
     * @param key
     */
    public static long deleteAllKey(String key) {
        Set<String> keys = cacheUtils.redisTemplate.keys(key + "*");
        return cacheUtils.redisTemplate.delete(keys);
    }


    /**
     * 获取String
     *
     * @param key
     * @return
     */
    public static Object getString(String key) {
        return cacheUtils.redisTemplate.opsForValue().get(key);
    }

    /**
     * 获取模糊的size
     *
     * @param key
     * @return
     */
    public static int getScanString(String key) {
        return cacheUtils.redisTemplate.keys(key).size();
    }

    /**
     * 获取模糊的String
     *
     * @param key
     * @return
     */
    public static List<String> getScanStringList(String key) {
        Set<String> set = cacheUtils.redisTemplate.keys(key);
        List<String> list = new ArrayList<String>(set);
        return list;
    }

    /**
     省略 N 多方法
     。。。。。。
     */
}
